package com.sondon.mayi.activity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.sondon.mayi.adapter.AddressListAdapter;
import com.sondon.mayi.configs.URLConfigs;
import com.sondon.mayi.entity.AddressInfo;
import com.sondon.mayi.entity.AddressManagerEntity;
import com.sondon.mayi.events.CheckboxEvent;
import com.sondon.mayi.fruit.R;
import com.sondon.mayi.ui.MyMultipleTopBar;
import com.sondon.mayi.ui.TopBarClickListener;
import com.sondon.mayi.util.AsyncHttpUtil;
import com.sondon.mayi.util.GsonUtil;
import com.sondon.mayi.util.LogUtil;
import com.sondon.mayi.util.ToastUtils;

/**
 * @Project : Fruit
 * @Package : com.sondon.mayi.activity
 * @Class : AddressManagerActivity
 * @Author : 蔡文锋
 * @DateTime：2015年6月14日 下午3:34:43
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { 收获地址管理 }
 *
 */
@EActivity(R.layout.address_manager)
public class AddressManagerActivity extends BaseActivity {
	public final static String ADDRESSINFO="addressinfo";
	@ViewById
	MyMultipleTopBar topBar;
	@ViewById
	Button add_address;//添加收货地址按钮
	@ViewById
	ListView address_list;//收货地址列表 
	@ViewById
	LinearLayout inner;//收货地址列表 暂时没用到
	@Bean
	AddressListAdapter adapter;//地址列表适配器
	
	//结果码  
	public final  static int resultCode=0X11;//没有选择直接返回
	public final  static int resultCode2=0X22;//有选择
	
	
	
	@AfterViews
	public void initView(){
		topBar.setTopBarClickListener(new TopBarClickListener() {
			
			@Override
			public void rightBtnClick() {
				
			}
			
			@Override
			public void leftBtnClick() {
				if(myprefs.is_forResult().get()){
					//设置forResult状态为false
					myprefs.is_forResult().put(false);
					
					//获取启动该activity之前的activity对应的Intent
					Intent mIntent=getIntent();
					// 设置结果，并进行传送  
					AddressManagerActivity.this.setResult(resultCode, mIntent);
					AddressManagerActivity.this.finish();
				}else{
					onBackPressed();
				}
			}
		});
		
		//设置适配器
		address_list.setAdapter(adapter);
		//发送收货地址列表请求
		initAddressList();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		//发送收货地址列表请求
		initAddressList();
	}
	
	@Override
	public void onBackPressed() {
		if(myprefs.is_forResult().get()){
			//设置forResult状态为false
			myprefs.is_forResult().put(false);
			
			//获取启动该activity之前的activity对应的Intent
			Intent mIntent=getIntent();
			// 设置结果，并进行传送  
			AddressManagerActivity.this.setResult(resultCode, mIntent);
			AddressManagerActivity.this.finish();
		}else{
			super.onBackPressed();
		}
		
	}
	
	//列表item被点击
	@ItemClick(R.id.address_list)
	public void itemClick(int position){
//		ToastUtils.show(context, "itemClick...");
		AddressInfo addressInfo=(AddressInfo) adapter.getItem(position);
		Bundle bundle=new Bundle();
		bundle.putSerializable(ADDRESSINFO, addressInfo);
		openActivity(AddressShowActivity_.class,bundle);

	}
	
 	@Click({R.id.add_address})
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.add_address:
			openActivity(AddAddressActivity_.class);
			break;
			
		default:
			break;
		}
	}
 	
 	
 	//发送收货地址列表请求
 	public void initAddressList(){
 		AsyncHttpUtil.get(URLConfigs.AddressList_ukey+myprefs.Ukey().get(), null, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int status, Header[] arg1, String json) {
				LogUtil.e(TAG, "AdrressManager json :"+json);
				if(status == 200){
					AddressManagerEntity entity=GsonUtil.GetFromJson(json, AddressManagerEntity.class);
					if(!entity.isStatus()){//没有数据
						ToastUtils.show(context, "暂无收货地址，请添加！");
					}else{
						adapter.setDatas(entity.getList());
					}
				}
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
				ToastUtils.show(context, R.string.network_unavailable);
			}
		});
 	}
 	
 	//地址被选择eventbus事件
 	public void onEventMainThread(CheckboxEvent event) {
 		super.onEventMainThread(event);
 		int position=event.getPosition();
 		//设置forResult状态为false
		myprefs.is_forResult().put(false);
		
		//获取启动该activity之前的activity对应的Intent
		Intent mIntent=getIntent();
		
		Bundle data=new Bundle();
		data.putString("address_id", adapter.GetDatas().get(position).getId());
		data.putString("receiver", adapter.GetDatas().get(position).getAccept_name());
		data.putString("postcode", adapter.GetDatas().get(position).getZip());
		data.putString("mobile", adapter.GetDatas().get(position).getMobile());
		data.putString("province", adapter.GetDatas().get(position).getProvince());
		data.putString("city", adapter.GetDatas().get(position).getCity());
		data.putString("area", adapter.GetDatas().get(position).getArea());
		data.putString("address", adapter.GetDatas().get(position).getAddress());
		
		String city=adapter.GetDatas().get(position).getCity_name();
		if("市辖区".equals(city)||"县".equals(city)){
			city="";
		}
		
		data.putString("address_all", adapter.GetDatas().get(position).getProvince_name()+
								  city+
								  adapter.GetDatas().get(position).getArea_name()+
								  adapter.GetDatas().get(position).getAddress()
				);
		mIntent.putExtras(data);
		// 设置结果，并进行传送 
		AddressManagerActivity.this.setResult(resultCode2, mIntent);
		AddressManagerActivity.this.finish();
 	}
}
